package com.lihepeng.leecode.tree;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 二叉树的右视图
 *
 */
public class Solution199 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root ==null) {
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i=0;i<size;i++) {
                TreeNode treeNode = queue.poll();
                if (i==size-1) {
                    result.add(treeNode.val);
                }
                if (treeNode.left!=null) {
                    queue.offer(treeNode.left);
                }
                if (treeNode.right!=null) {
                    queue.offer(treeNode.right);
                }

            }
        }

        return result;
    }
    @Test
    public void runTest() {
        TreeNode root = new TreeNode(0);

        TreeNode l1 = new TreeNode(1);
        TreeNode r1 = new TreeNode(2);
        TreeNode l2 = new TreeNode(3);
        TreeNode r2 = new TreeNode(4);
        root.left = l1;
        root.right = r2;
        r1.left = l2;
        r1.right = r2;
        List<Integer> integers = this.rightSideView(root);
        System.out.println(integers);

    }
}
